import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QPushButton,
    QLabel,
    QLineEdit,
    QMessageBox,
    QProgressDialog,
)

from PyQt5.QtCore import Qt


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.init_UI()

    def init_UI(self):
        self.resize(300, 150)
        self.setWindowTitle("进度对话框")

        self.lab = QLabel("文件数量", self)
        self.lab.move(20, 40)

        self.edit = QLineEdit("10000", self)
        self.edit.move(100, 40)

        self.bt = QPushButton("开始", self)
        self.bt.move(20, 80)

        self.bt.clicked.connect(self.showDialog)

        self.show()

    def showDialog(self):
        num = int(self.edit.text())
        progress = QProgressDialog(self)
        progress.setWindowTitle("请稍等")
        progress.setLabelText("正在操作...")
        progress.setCancelButtonText("取消")
        progress.setMinimumDuration(5)
        progress.setWindowModality(Qt.WindowModal)
        progress.setRange(0, num)
        for i in range(num):
            progress.setValue(i)
            if progress.wasCanceled():
                QMessageBox.warning(self, "提示", "操作失败")
                break
            elif i == num - 1:
                progress.setValue(num)
                QMessageBox.information(self, "提示", "操作成功")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
